package cn.itsource.pethome.service.impl;

import cn.itsource.pethome.constant.Constant;
import cn.itsource.pethome.domain.Employee;
import cn.itsource.pethome.domain.Shop;
import cn.itsource.pethome.mapper.EmployeeMapper;
import cn.itsource.pethome.mapper.ShopMapper;
import cn.itsource.pethome.query.ShopQuery;
import cn.itsource.pethome.service.IShopService;
import cn.itsource.pethome.utils.EmailUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.mail.internet.MimeMessage;
import java.io.File;

@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class ShopServiceImpl extends BaseServiceImpl<Shop,ShopQuery> implements IShopService {
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private EmailUtils emailUtils;

    /**
     * 商家入驻
     * @param shop
     */
    @Override
    @Transactional
    public void settledIn(Shop shop) {
        //1.再插入一行数据到t_employee表  返回主键值
        Employee admin = shop.getAdmin();
        admin.setState(Constant.AUDIT);          //员工状态默认设置为1 待审核状态
        admin.setJob_id(6L);        //员工职位默认设置为网管
        admin.setPhone(shop.getTel());
        employeeMapper.save(admin);

        //2.先插入一行数据到t_shop表 返回主键值
        shopMapper.save(shop);

        //3.再修改t_employee表的shop_id外键列的值
        admin.setShop_id(shop.getId());
        employeeMapper.update(admin);
    }

    /**
     * 商家入驻审核
     * @param id
     * @param auditState
     */
    @Override
    public void audit(Long id, Integer auditState) {
        //先通过id查询商家信息
        Shop shop = shopMapper.findOne(id);
        //1.修改t_shop表当前行的状态为2 待激活
        shop.setState(Constant.ACTIVITY);
        shopMapper.update(shop);

        //2.修改t_employee表的数据状态2 待激活
        shop.getAdmin().setState(Constant.ACTIVITY);
        employeeMapper.update(shop.getAdmin());

        //3.发送激活邮件
        emailUtils.send(shop);
    }


}
